package cn.jetpiece.cloud.joss.app.modular.object.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.jetpiece.cloud.joss.api.object.RegionApi;
import cn.jetpiece.cloud.joss.api.object.model.params.CreateRegionParam;
import cn.jetpiece.cloud.joss.api.object.model.params.UpdateRegionParam;
import cn.jetpiece.cloud.joss.api.object.model.query.RegionQuery;
import cn.jetpiece.cloud.joss.api.object.model.result.RegionResult;
import cn.jetpiece.cloud.joss.app.modular.object.entity.Region;
import cn.jetpiece.cloud.joss.app.modular.object.service.RegionService;
import lombok.extern.slf4j.Slf4j;
import cn.jetpiece.cloud.model.contants.ResponseMsgConstants;
import cn.jetpiece.cloud.model.page.PageResult;
import cn.jetpiece.cloud.model.response.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * 域控制器
 *
 * @author XuJZ
 * @since 2020-09-05 15:41:52
 */
@Slf4j
@RestController
public class RegionController implements RegionApi {

    @Autowired
    private RegionService regionService;

    @Override
    public Result<String> add(CreateRegionParam param) {
        if (regionService.add(param)) {
            return Result.OK(ResponseMsgConstants.CREATE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.CREATE_ERROR);
        }
    }

    @Override
    public Result<String> update(UpdateRegionParam param) {
        if (regionService.update(param)) {
            return Result.OK(ResponseMsgConstants.UPDATE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.UPDATE_ERROR);
        }
    }

    @Override
    public Result<String> delete(String id) {
        if (regionService.delete(id)) {
            return Result.OK(ResponseMsgConstants.DELETE_SUCCESS);
        } else {
            return Result.error(ResponseMsgConstants.DELETE_ERROR);
        }
    }

    @Override
    public Result<RegionResult> get(String id) {
        Region bean = regionService.getById(id);
        if (bean == null) {
            return Result.OK();
        }
        RegionResult result = new RegionResult();
        BeanUtil.copyProperties(bean, result);
        return Result.OK(result);
    }

    @Override
    public Result<PageResult<RegionResult>> page(RegionQuery query) {
        return Result.OK(regionService.page(query));
    }

}
